﻿Public Class frmFile
    Dim mPrjOpt As LevelDatabase.CurrentOption

    Private Sub frmReadOpt_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPrepare()

    End Sub
    Private Sub LoadPrepare()
        'Dim mDB As New DbLevel
        mPrjOpt = MyShared.myDb.CurrentRead()
        If mPrjOpt.CurrentProject = String.Empty Then
            MsgBox("请设置当前工程！")
            Me.Close()
        Else
            lblPrj.Text = mPrjOpt.CurrentProject
            txtWeather.Text = mPrjOpt.Weather
        End If
        DisplayExistFile()
        InputPanel1.Enabled = True
        txtCompareFile.Focus()
    End Sub

    Private Sub txtCompareFile_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCompareFile.GotFocus
        lstPrjFl.Items.Clear()
        lstPrjFl.Items.Add("比对文件：")
        DisplayExistFile()
    End Sub
    Private Sub DisplayExistFile()
        Dim pf As New PathAndFile(mPrjOpt.CurrentProject)
        Dim sfile() As String = PathAndFile.GetExistFiles.Trim.Split(",")
        If sfile.Length = 1 And sfile(0).Length < 2 Then

        Else
            Array.Sort(sfile)
            For Each mfile As String In sfile
                Dim Exist As Boolean = False
                If mfile.Length > 0 Then
                    For Each lst As String In lstPrjFl.Items
                        If lst = mfile Then
                            Exist = True
                            Exit For
                        End If
                    Next
                    If Not Exist Then lstPrjFl.Items.Add(mfile)
                End If
            Next
            If lstPrjFl.Items.Count > 3 Then lstPrjFl.TopIndex = lstPrjFl.Items.Count - 4
        End If
    End Sub

    Private Sub txtSaveTo_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtSaveTo.GotFocus
        lstPrjFl.Items.Clear()
        lstPrjFl.Items.Add("数据记录到：")
        DisplayExistFile()
        If lstPrjFl.Items.Count > 4 Then
            For I As Integer = 2 To lstPrjFl.Items.Count - 4
                lstPrjFl.Items.RemoveAt(1)
            Next
        End If
        If lstPrjFl.Items.Count = 1 Then
            txtSaveTo.Text = Now.ToString("yyMMdd") & "-" & "000"
        End If
    End Sub

    ''' <summary>
    ''' 选择已有工程或已有文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstPrjFl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPrjFl.SelectedIndexChanged

        If lstPrjFl.Items(0) = "比对文件：" Then
            If lstPrjFl.SelectedIndex > 0 Then
                txtCompareFile.Text = lstPrjFl.SelectedItem
                SetSaveFileText()
            End If
        ElseIf lstPrjFl.Items(0) = "数据记录到：" Then
            If lstPrjFl.SelectedIndex > 0 Then
                txtSaveTo.Text = lstPrjFl.SelectedItem
            End If
        ElseIf lstPrjFl.Items(0) = "可选天气:" Then
            If lstPrjFl.SelectedIndex > 0 Then
                txtWeather.Text = lstPrjFl.SelectedItem
            End If
        End If

    End Sub

    ''' <summary>
    ''' 根据比对文件，设置保存文件的文件名
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetSaveFileText()
        Dim sTime As Integer = 0
        If txtCompareFile.Text.Length > 0 Then
            Dim sLastTurn As String = txtCompareFile.Text
            Dim pos As Integer = InStr(sLastTurn, "-")
            If pos > 0 Then
                sTime = CInt(Mid(sLastTurn, pos + 1)) + 1
            End If
        End If
        txtSaveTo.Text = Today.ToString("yyMMdd") & "-" & String.Format("{0:D3}", sTime)
        txtSaveTo.SelectionStart = txtSaveTo.Text.Length
        'txtSaveTo.Focus()
    End Sub

    Private Sub txtWeather_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWeather.GotFocus
        lstPrjFl.Items.Clear()
        With lstPrjFl.Items
            .Add("可选天气:")
            .Add("晴")
            .Add("多云")
            .Add("阴")
            .Add("小雨")
            .Add("雪")
        End With

    End Sub



    Private Sub frmReadOpt_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        SaveSet()
    End Sub
    Private Sub bReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        SaveSet()
        Me.Close()
    End Sub
    Private Sub SaveSet()
        With mPrjOpt
            .CompareFile = txtCompareFile.Text
            .RecordFile = txtSaveTo.Text
            .Weather = txtWeather.Text
        End With
        'Dim mDb As New DbLevel
        MyShared.myDb.CurrentRecord(mPrjOpt)

    End Sub

 
End Class